using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;

namespace LenninSoftware.FormEditor.Adapters
{
    public class Boolean : BaseControlAdapter
    {

        public override Control CreateReadOnlyControl(FieldInfo field)
        {
            Label label = new Label();
            return label;
        }

        public override void LoadReadOnlyData(Control control, SPListItem item, object data)
        {
            Label label = (Label)control;
            label.Text = (string)data;
        }

        public override System.Web.UI.Control CreateControl(FieldInfo field)
        {
            DropDownList ddl = new DropDownList();

            ddl.Items.Add(String.Empty);

            ddl.Items.Add("Yes");
            ddl.Items.Add("No");

            return ddl;
        }

        public override void LoadData(System.Web.UI.Control control, SPListItem item, object data)
        {

            if (data == null)
                return;

            DropDownList ddl = (DropDownList)control;
            bool booleanValue = (bool)data;
            ddl.SelectedValue = booleanValue ? "Yes" : "No";

        }

        public override object SaveData(System.Web.UI.Control control, SPListItem item, object currentData)
        {
            DropDownList ddl = (DropDownList)control;
            return (ddl.SelectedValue == "Yes");
        }

        public override void Reset(System.Web.UI.Control control)
        {
            DropDownList ddl = (DropDownList)control;
            ddl.ClearSelection();
        }

        public override bool isEmpty(Control control)
        {
            DropDownList ddl = (DropDownList)control;
            return ddl.SelectedIndex == -1;
        }
    }
}
